procedure tsort; {A - Matrix; List - Result}
var num,i:longint; was:array[1..10001] of boolean;

procedure dfs(v:longint);
var j:longint;
begin
was[v]:=true;
for j:=1 to n do if (a[v][j]<>0) and (not was[j]) then dfs(j);
inc(num);
list[n-num+1]:=v;
end;

begin
fillchar(list,sizeof(list),0);
fillchar(was,sizeof(was),false);
num:=0;
for i:=1 to n do if not was[i] then dfs(i);
end;